﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KP_CommonLibrary.KP_DataAccess;
using System.Data;
using Microsoft.ApplicationBlocks.Data;
using KP_CommonLibrary.KP_ObjectReferences;
using System.Data.SqlClient;
namespace KEEPER_BRR_13dotNET.DataAccess
{
    public class DAL_KPDangKyKD : KP_DALListBase
    {
        public DAL_KPDangKyKD()
            : base("tblDangKyKD")
        {
        }
        public void DuyetHoSo(string sttRec, int ma_tt, int nextMa_Tt, string lydo, string user0, string client0, int type)
        {
            int i = SqlHelper.ExecuteNonQuery(KP_OCnnSql.Cnn_String, "kp_DuyetHoSo", sttRec, ma_tt, nextMa_Tt, lydo, user0, client0, type);
        }
        public void KetThucDuyetHs(string sttRec, int ma_tt, int nextMa_Tt, string lydo, string user0, string client0)
        {
            int i = SqlHelper.ExecuteNonQuery(KP_OCnnSql.Cnn_String, "kp_DangKyKDKetThucDuyetHS", sttRec, ma_tt, nextMa_Tt, lydo, user0, client0);
        }
        
        public void TraHoSo(string sttRec, string user0, string client0)
        {
            int i = SqlHelper.ExecuteNonQuery(KP_OCnnSql.Cnn_String, "KP_DangKyKDTraHoSo", sttRec, user0, client0);
        }
        public DataTable FindDangKyBySoBn(string so_Bn)
        {
            return SqlHelper.ExecuteDataTable(KP_OCnnSql.Cnn_String, "[kp_FindDangKyBySoBn]", so_Bn);

        }
        public DataTable FindDangKyKDBySttRec(string sttRec)
        {
            return SqlHelper.ExecuteDataTable(KP_OCnnSql.Cnn_String, "[kp_FindDangKyBySttRec]", sttRec);

        }
        public bool KiemTraSoCmnd(string so_Cmnd, out string message)
        {
            message = "";
            SqlParameter p1 = new SqlParameter("@So_Cmnd", SqlDbType.NVarChar, 12);
            p1.Value = so_Cmnd;
            SqlParameter p2 = new SqlParameter("@KetQua", SqlDbType.Int, 0);
            p2.Direction = ParameterDirection.Output;
            SqlParameter p3 = new SqlParameter("@ThongBao", SqlDbType.NVarChar, 256);
            p3.Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(KP_OCnnSql.Cnn_String, CommandType.StoredProcedure
                , "[kp_KiemTraSoCmnd]", p1, p2, p3);
            if (p3.Value != DBNull.Value)
                message = p3.Value.ToString();
            return p2.Value != DBNull.Value && Convert.ToBoolean(p2.Value);


        }

        public DataTable FindDangKyKdByKeyword(string keyWord, int trangthai1, int trangthai2)
        {
            return SqlHelper.ExecuteDataTable(KP_OCnnSql.Cnn_String, "[kp_FindDangKyKdByKeyword]", keyWord, trangthai1, trangthai2, false);
        }

        public DataTable FindDangKyByMaHs(string maHs)
        {
            return SqlHelper.ExecuteDataTable(KP_OCnnSql.Cnn_String, "[kp_DangKyKDSelectByMaHs]", int.Parse(maHs));

        }
        public bool KiemTraTruocKhiCapLai(string masoKd, out string message)
        {
            message = "";
            //Kiểm tra xem có tồn tại không.
            //Kiểm tra xem có 
            try
            {
                SqlParameter p1 = new SqlParameter("@So_Gp", SqlDbType.NVarChar, 15);
                p1.Value = masoKd;
                SqlParameter p2 = new SqlParameter("@Ket_Qua", SqlDbType.Int);
                //p2.Value = DBNull.Value;
                p2.Direction = ParameterDirection.Output;
                SqlParameter p3 = new SqlParameter("@Thong_Bao", SqlDbType.NVarChar, 255);
                //p3.Value = DBNull.Value;
                p3.Direction = ParameterDirection.Output;

                SqlHelper.ExecuteNonQuery(KP_OCnnSql.Cnn_String, CommandType.StoredProcedure, "[kp_KiemTraTruocKhiCapLaiGp]", new SqlParameter[] { p1, p2, p3 });
                if ((int)p2.Value != 1)
                {
                    message = p3.Value.ToString();
                    return false;
                }
                else
                    return true;

            }
            catch (Exception ex)
            {
                throw new Exception("Kiểm tra trước khi cấp lại gặp lỗi :\n" + ex.Message, ex);
            }
        }

        public void DeleteHoSo(string sttRec, SqlConnection conn, SqlTransaction tran)
        {
            SqlHelper.ExecuteNonQuery(tran, "kp_CtNganhKdDelete", new object[] { sttRec });
            SqlHelper.ExecuteNonQuery(tran, "kp_CtTPHoSoDelete", new object[] { sttRec,-1 });
            SqlHelper.ExecuteNonQuery(tran, "kp_CtThanhVienDelete", new object[] { sttRec });            
            SqlHelper.ExecuteNonQuery(tran, "kp_DangKyKDDelete", new object[] { sttRec });
        }
    }
}
